﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGE;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace YouOnlyGetOne
{
    public class GameObject : EngineObject
    {
        public Vector2 drawPosition = Vector2.Zero;
        public PlayScreen ps;
        public static Texture2D tile = Engine.Load<Texture2D>("Images\\tile");

        // aux
        public Vector2 speed = new Vector2(0, 0);

        public GameObject(PlayScreen ps, Texture2D sprite)
            : base(sprite)
        {
            this.ps = ps;
        }

        public override void Update(GameTime gameTime)
        {
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (ps.player == null || sprite == null) return;

            drawPosition = position + ps.player.CameraOffset;
            spriteBatch.Draw(sprite, drawPosition, sourceRectangle, Color.White, rotation, origin, scale, SpriteEffects.None, 0);

            if (YOGO.isDebugMode) DrawHitBox(spriteBatch);
            //DrawCollidingTiles(spriteBatch);
        }

        public void DrawHitBox(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(GameScreenManager.blackScreen,
                             new Vector2((int)(HitBox.Location.X + ps.player.CameraOffset.X), (int)(HitBox.Location.Y + ps.player.CameraOffset.Y)),
                //drawPosition - new Vector2(HitBox.Width, HitBox.Height) / 2, 
                             HitBox,
                             new Color(0, 0, 0, .5f));
        }

        public void DrawCollidingTiles(SpriteBatch spriteBatch)
        {
            ps.map.GetCollidingTiles(this).ForEach(p => spriteBatch.Draw(tile,
                                                                            (new Vector2(p.X * ps.map.TileWidth, p.Y * ps.map.TileHeight)) + ps.player.CameraOffset,
                                                                            new Color(1, 1, 1, .5f)));
        }

        /// <summary>
        /// Retorna se colidiu ou não com algum tile da camada. Se colidiu ajusta a posição.
        /// </summary>
        public virtual bool CheckCollisionWithMap(String tileLayerName)
        {
            bool collided = false;

            // update Position.X
            if (speed.X != 0)
            {
                position.X += speed.X;
                float dH = MapManager.CollisionH(this, ps.map, tileLayerName, speed.X < 0);
                if (dH != 0)
                {
                    position.X += dH;
                    collided = true;
                }
                speed.X = 0;
            }

            // update Position.Y
            if (speed.Y != 0)
            {
                position.Y += speed.Y;
                float dV = MapManager.CollisionV(this, ps.map, tileLayerName, speed.Y < 0);
                if (dV != 0)
                {
                    position.Y += dV;
                    collided = true;
                }
                speed.Y = 0;
            }

            return collided;
        }
    }
}
